#! /bin/bash
# 只用一个盒子（必须与初始化配置保持一致）或者不用盒子可以使用localhost 多个盒子必须ipv4地址
ip=localhost
P=61002

start()
{
    tmp=`ps -ef | grep "./RUN1/RUN1 -ip ${ip} -P ${P}" | wc -l`
    if [ $tmp -gt 1 ]; then 
      echo "The server arealdy started...abort!" 
      exit 1 
    fi 

    rm logs/RUN1${P}.log
    rm errs/RUN1${P}.log
    sleep 2

    echo "start RUN1${P}"
    nohup ./RUN1/RUN1 -ip ${ip} -P ${P} 1>logs/RUN1${P}.log 2>errs/RUN1${P}.log &
    echo "ok!"
    sleep 1

    ps -ef | grep RUN1
}


stop() {
    gre_str="RUN1 -ip ${ip} -P ${P}"
    echo ${gre_str}
    k_pid=$(ps -ef | grep "${gre_str}" |grep -v grep|awk '{printf $2 "\n"}')
    for i in $k_pid
    do
    echo $i 
    $(kill -9 $i)
    done
}



usage()
{
        echo "Usage: $0 [start|stop|restart]"
}

if [ $# -lt 1 ];then
        usage
        exit
fi

if [ "$1" = "start" ];then
        start

elif [ "$1" = "stop" ];then
        stop

elif [ "$1" = "restart" ];then
        stop
        start

else
        usage
fi
